/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewDecAlgJPanel.java
 *
 * Created on Jun 24, 2011, 5:56:08 AM
 */
package presentation.guiForDataVisualization;

import business.algorithm.technicalIndicator.AbstractTechnicalIndicator;
import java.util.TreeMap;
import javax.swing.JDialog;
import presentation.ParameterJPanel;

/**
 *
 * @author Dinh
 */
public class AddNewTechIndicatorJPanel extends javax.swing.JPanel {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private JDialog parent;
    private boolean ok = false;

    public boolean isOk() {
        return ok;
    }

    /**
     * Creates new form AddNewDecAlgJPanel
     */
    public AddNewTechIndicatorJPanel(JDialog jDialog) {
        this.parent = jDialog;
        initComponents();
        initOtherComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelJButton = new javax.swing.JButton();
        okJButton = new javax.swing.JButton();
        techIndicatorJComboBox = new javax.swing.JComboBox();
        techIndicatorParametersContainerJPanel = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Technical Indicator", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        okJButton.setText("OK");
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        techIndicatorJComboBox.setModel(new javax.swing.DefaultComboBoxModel(business.algorithm.technicalIndicator.TechnicalIndicatorAPI.TECHNICAL_INDICATOR_LIST));
        techIndicatorJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                techIndicatorJComboBoxActionPerformed(evt);
            }
        });

        techIndicatorParametersContainerJPanel.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(techIndicatorJComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addComponent(okJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(cancelJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(techIndicatorParametersContainerJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(techIndicatorJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(techIndicatorParametersContainerJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okJButton)
                    .addComponent(cancelJButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cancelJButton, okJButton});

    }// </editor-fold>//GEN-END:initComponents

    private void techIndicatorJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_techIndicatorJComboBoxActionPerformed
        techIndicatorParametersContainerJPanel.removeAll();
        technicalIndicator = business.algorithm.technicalIndicator.TechnicalIndicatorAPI.getTechnicalIndicator((String) techIndicatorJComboBox.getSelectedItem());
        techIndicatorParameterJPanel = new ParameterJPanel(technicalIndicator.getParameterList());
        techIndicatorParametersContainerJPanel.add(techIndicatorParameterJPanel);
        techIndicatorParametersContainerJPanel.updateUI();
    }//GEN-LAST:event_techIndicatorJComboBoxActionPerformed

    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okJButtonActionPerformed
        this.ok = true;

        technicalIndicator = business.algorithm.technicalIndicator.TechnicalIndicatorAPI.getTechnicalIndicator((String) techIndicatorJComboBox.getSelectedItem());

        // Add new prediction algorithm
        TreeMap<String, Object> valueMap = new TreeMap<String, Object>();
        for (String name : technicalIndicator.getParameterList().keySet()) {
            valueMap.put(name, techIndicatorParameterJPanel.getValue(name));
        }
        technicalIndicator.setParameterValue(valueMap);

        this.parent.dispose();
    }// GEN-LAST:event_okJButtonActionPerformed

    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelJButtonActionPerformed
        this.ok = false;

        this.parent.dispose();
    }// GEN-LAST:event_cancelJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelJButton;
    private javax.swing.JButton okJButton;
    private javax.swing.JComboBox techIndicatorJComboBox;
    private javax.swing.JPanel techIndicatorParametersContainerJPanel;
    // End of variables declaration//GEN-END:variables
    private AbstractTechnicalIndicator technicalIndicator;
    private ParameterJPanel techIndicatorParameterJPanel;

    public AbstractTechnicalIndicator getPreAlg() {
        return technicalIndicator;
    }

    public JDialog getParentDialog() {
        return parent;
    }

    private void initOtherComponents() {
        technicalIndicator = business.algorithm.technicalIndicator.TechnicalIndicatorAPI.getTechnicalIndicator((String) techIndicatorJComboBox.getSelectedItem());
        techIndicatorParameterJPanel = new ParameterJPanel(technicalIndicator.getParameterList());
        techIndicatorParametersContainerJPanel.add(techIndicatorParameterJPanel);
    }
}
